<?php
namespace app\common\validate;

use think\Validate;
use think\Db;
class MouldsFieldValidate extends Validate
{
    protected $rule =   [
        'ename'  => 'require|alphaDash|min:3|max:20|checkName',
        'cname'  => 'require|max:25',
        'model_id' => 'require|number',
        'type' => 'require|number',
        'validate' => 'require|number',
        'descript' => 'require|max:200',
		'sort'	=> 'require|number'
    ];
    
    protected $message  =   [
        'ename.require' => '字段必须填写',
        'ename.min'     => '字段名称最少不能少于3个字符',
        'ename.alphaDash'     => '字段名称字母和数字，下划线_及破折号-符',
        'ename.max'     => '字段名称最多不能超过20个字符',
		'ename.checkName'  => '字段名称已存在',
		'cname.require' => '字段标题必须填写',
        'cname.max'     => '字段名称最多不能超过25个字符',
        'cname.unique'     => '字段标题已存在',
        'validate.require'=> '是否验证必须填写',
        'validate.number'=> '是否验证必须数字',
        'descript.require'=> '描述必须填写',
        'descript.max'  => '描述最多不能超过200个字符',
		'sort.require' => '必须输入排序',
		'sort.number' => '必须输入数字'
    ];
    
    protected $scene = [
        'add'  =>  ['ename','cname','descript','sort'],
        'edit'  =>  ['ename','cname','descript','sort'],
		'sort' => ['sort']
    ];
	//检测字段是否重复
	protected function checkName($value, $rule, $data=[])
	{
	    $rest = Db::name('models_field')->where(['model_id'=>$data['model_id'],'ename'=>$data['ename']])->find();
		if(empty($rest)){
			return true;
		}else{
			return false;
		}
	}
}
